﻿using Red.Controls.DataDriven;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Red.Controls
{
    public class ImageCanvas : Canvas
    {
        public static readonly DependencyProperty CanvasImageSourceProperty =
                DependencyProperty.Register( "CanvasImageSource", typeof( ImageSource ), typeof( ImageCanvas ), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.AffectsRender ) );

        public ImageSource CanvasImageSource
        {
            get { return (ImageSource)GetValue( CanvasImageSourceProperty ); }
            set { SetValue( CanvasImageSourceProperty, value ); }
        }

        protected override void OnRender( DrawingContext dc )
        {
            dc.DrawImage( CanvasImageSource, new Rect( RenderSize ) );
            base.OnRender( dc );
        }
    }

    /// <summary>
    /// Interaction logic for RedThumbnail.xaml
    /// </summary>
    public class RedThumbnail : ElementFacade<ImageCanvas>
    {
        public static readonly DependencyProperty ImageProperty = 
            DependencyProperty.Register( "Image", typeof( ImageSource ), typeof( RedThumbnail ), new FrameworkPropertyMetadata( null, ImagePropertyChanged ) );

        public ImageSource Image
        {
            get { return (ImageSource)GetValue( ImageProperty ); }
            set { SetValue( ImageProperty, value ); }
        }

        public RedThumbnail()
        {
        }

        public static void ImagePropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs args )
        {
            var thumbnail = d as RedThumbnail;
            thumbnail.Element.CanvasImageSource = args.NewValue as ImageSource;
        }
    }
}
